package com.foreveross.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.common.Constants;
import com.foreveross.dao.DDManagerDAO;
import com.foreveross.dao.HrHouseInfoDAO;
import com.foreveross.vo.ExpressCompany;
import com.foreveross.vo.HrCode;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.ProductType;
import com.foreveross.vo.SysCodeHouse;
import com.foreveross.vo.common.ResultVo;

@Service("ddManagerService")
@Transactional
public class DDManagerService {

	@Resource
	DDManagerDAO ddManagerDAO;
	
	@Resource
	HrHouseInfoDAO hrHouseInfoDAO;

	public <T> void saveOrUpdate(T entity, String type) throws Exception {
		if (type.equals(Constants.UPDATE_ADD)) {
			ddManagerDAO.save(entity);
		} else if (type.equals(Constants.UPDATE_EDIT)) {
			ddManagerDAO.updateObject(entity);
		}

	}

	public Object getObjectById(Class clazz, String id) throws Exception {
		return ddManagerDAO.findById(clazz, id);
	}

	public Object getObjectByIntId(Class clazz, int id) throws Exception {
		return ddManagerDAO.findByIntId(clazz, id);
	}

	public JSONArray getCodeSelect(String type, String typeName, String id)
			throws Exception {
		List<HrCode> list = ddManagerDAO.getCodeList(type, typeName,null);
		JSONArray array = new JSONArray();
		if (id != null && !"".equals(id)) {
			for (int i = 0; i < list.size(); i++) {
				HrCode c = list.get(i);
				if (c.getCodeId().equals(id))
					c.setSelected(true);
			}
		}
		return JSONArray.fromObject(list);
	}

	public Map<String, Object> getCodeList(String type, String typeName,String houseId)
			throws Exception {
		List<HrCode> list = ddManagerDAO.getCodeList(type, typeName,houseId);
		List<Map<String, Object>> listv = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		for (HrCode hc : list) {
			Map<String, Object> mapO = new HashMap<String, Object>();
			mapO.put("chooseItem", hc.getCodeDesc());
			mapO.put("chooseValue", hc.getCodeId());
			listv.add(mapO);
		}

		map.put("data", listv);
		map.put("total", listv.size());
		return map;
	}

	public List<HrCode> getCodeList(String type, String typeName,
			String codeDesc,String houseId, int pageIndex, int pageSize) throws Exception {
		return ddManagerDAO.findCodeList(type, typeName, codeDesc,houseId, pageIndex,
				pageSize);
	}

	public long getCodeListCount(String type, String typeName, String codeDesc,String houseId)
			throws Exception {
		return ddManagerDAO.findCodeListCount(type, typeName, codeDesc,houseId);
	}

	public void deleteCodeList(String codeIds) throws Exception {
		for (String codeId : codeIds.split(",")) {
			ddManagerDAO.deleteCode(codeId);
		}
	}

	public long getProductTypeListCount(ProductType productType)
			throws Exception {
		return ddManagerDAO.findProductTypeListCount(productType);
	}

	public List<ProductType> getProductTypeList(ProductType productType,
			int pageIndex, int pageSize) throws Exception {
		return ddManagerDAO.findProductTypeList(productType, pageIndex,
				pageSize);
	}

	public void deleteProductTypeList(String ids) throws Exception {
		for (String id : ids.split(",")) {
			ddManagerDAO.deleteProductType(id);
		}
	}

	public long getExpressCompanyCount(String name) throws Exception {
		return ddManagerDAO.findExpressCompanyCount(name);
	}

	public List<ExpressCompany> getExpressCompanyList(String name,
			int pageIndex, int pageSize) throws Exception {
		return ddManagerDAO.findExpressCompanyList(name, pageIndex, pageSize);
	}
	
	public HrCode getCodeById(String id) throws Exception {
		return (HrCode) ddManagerDAO.findById(HrCode.class,id);
	}

	public void deleteExpressCompanyList(String ids) throws Exception {
		for (String id : ids.split(",")) {
			ddManagerDAO.deleteExpressCompanyList(id);
		}
	}

	public void findAllExpressCompany(ResultVo resultVo) throws Exception {
		List<ExpressCompany> list = ddManagerDAO.findAllExpressCompany();
		List listv = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			ExpressCompany hc = list.get(i);
			Map<String, String> map = new HashMap();
			map.put("chooseItem", hc.getName());
			map.put("chooseValue", hc.getId());
			listv.add(map);
		}
		if (listv != null && !listv.isEmpty()) {
			Map<String, Object> map = new HashMap();
			map.put("data", listv);
			map.put("total", listv.size());
			resultVo.setData(map);
			resultVo.setMessage("");
		} else {
			resultVo.setData(new ArrayList());
			resultVo.setMessage("快递公司信息为空！");
		}
		resultVo.setSuccess(true);
	}

	public ProductType getProductTypeList(String id) throws Exception {
		return (ProductType) ddManagerDAO.findById(ProductType.class,id);
	}

	public void updateSysCodeHouse(String codeId, String houseIds) throws Exception {
		ddManagerDAO.deleteSysCodeHouserByCode(codeId);
		for(String id :houseIds.split(",")){
			SysCodeHouse sysCodeHouse = new SysCodeHouse();
			sysCodeHouse.setHrCode(new HrCode(codeId));
			sysCodeHouse.setHrHouses(new HrHouses(id));
			ddManagerDAO.save(sysCodeHouse);
		}
	}

	public List<Map<String,Object>> getSysCodeHouse(String codeId,String updateType) throws Exception{
		//HrCode hrCode = (HrCode) ddManagerDAO.findById(HrCode.class, codeId);
		
		List<HrHouses> hrHousesList = hrHouseInfoDAO.findHouseList(null, null, null, 0, Constants.PAGE_MAX_SIZE, null);
		List<HrHouses> codeHousesList = ddManagerDAO.findHouseListByCode(codeId);
		List<Map<String,Object>> hrHousesMapList = new ArrayList<Map<String,Object>>();
		for(HrHouses hrHouses : hrHousesList){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("id", hrHouses.getId());
			map.put("text", hrHouses.getName());
			if(updateType.equals(Constants.UPDATE_EDIT)){
				map.put("diesabled", false);
				hrHouses.setChkDisabled(false);
			}else if(updateType.equals(Constants.UPDATE_WATCH)){
				map.put("diesabled", true);
				hrHouses.setChkDisabled(true);
			}
			for(HrHouses codeHouses : codeHousesList){
				if(codeHouses.getId().equals(hrHouses.getId())){
					hrHouses.setChecked(true);
					map.put("checked", true);
				}
			}
			hrHousesMapList.add(map);
		}
		
		return hrHousesMapList;
	}



}
